package com.ksd.pug.controller.role;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ksd.pug.commons.enums.ResultStatusEnumA;
import com.ksd.pug.commons.ex.PugValidationException;
import com.ksd.pug.commons.utils.fn.asserts.Vsserts;
import com.ksd.pug.controller.BaseController;
import com.ksd.pug.pojo.Permission;
import com.ksd.pug.pojo.Role;
import com.ksd.pug.service.permission.IPermissionService;
import com.ksd.pug.service.role.IRoleService;
import com.ksd.pug.vo.RolePermissionVo;
import com.ksd.pug.vo.user.RoleVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: hcl
 * @date: 2022/1/24 15:06
 * @Description:
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class RoleController extends BaseController {

    private final IRoleService roleService;
    private final IPermissionService permissionService;


    /**
     * 查询角色管理列表信息
     * 创建时间：2022-02-21 15:07:39
     * @return
     */
    @GetMapping("/role/load")
    public List<Role> findRoleList() {
        return roleService.findRoleList();
    }


    @GetMapping("/role/load2")
    public List<String> findRoleList2() {
        List<Role> roleList = roleService.findRoleList();
        List<String> collect = roleList.stream().map(Role::getName).collect(Collectors.toList());
        return collect;
    }
    /**
     * 查询角色管理列表信息并分页
     * 创建时间：2022-02-21 15:07:39
     */
    @PostMapping("/role/list")
    public IPage<Role> findRoles(@RequestBody RoleVo roleVo) {
        return roleService.findRolePage(roleVo);
    }


    /**
     * 根据角色管理id查询明细信息
     * 创建时间：2022-02-21 15:07:39
     */
    @GetMapping("/role/get/{id}")
    public Role getRoleById(@PathVariable("id") String id) {
        if(Vsserts.isEmpty(id)){
            throw new PugValidationException(ResultStatusEnumA.ID_NOT_EMPTY);
        }
        return roleService.getRoleById(new Long(id));
    }


    /**
     * 保存和修改角色管理
     * 创建时间：2022-02-21 15:07:39
     */
    @PostMapping("/role/saveupdate")
    public Role saveupdateRole(@RequestBody Role role) {
        return roleService.saveupdateRole(role);
    }


    /**
     * 根据角色管理id删除角色管理
     * 创建时间：2022-02-21 15:07:39
     */
    @PostMapping("/role/delete/{id}")
    public int deleteRoleById(@PathVariable("id") String id) {
        if(Vsserts.isEmpty(id)){
            throw new PugValidationException(ResultStatusEnumA.ID_NOT_EMPTY);
        }
        return roleService.deleteRoleById(new Long(id));
    }


    /**
     * 根据角色管理ids批量删除角色管理
     * 创建时间：2022-02-21 15:07:39
     */
    @PostMapping("/role/delBatch")
    public boolean delRole(@RequestBody RoleVo roleVo) {
        log.info("你要批量删除的IDS是:{}", roleVo.getBatchIds());
        if (Vsserts.isEmpty(roleVo.getBatchIds())) {
            throw new PugValidationException(ResultStatusEnumA.ID_NOT_EMPTY);
        }
        return roleService.delBatchRole(roleVo.getBatchIds());
    }

    /**
     * 添加角色对应的权限
     * @param rolePermissionVo
     * @return
     */
    @PostMapping("/role/savePermission")
    public boolean savePermissionRole(@RequestBody  RolePermissionVo rolePermissionVo) {
        return permissionService.savePermissionRole(rolePermissionVo);
    }

    /**
     * 查询角色对应权限
     * @param roleid
     * @return
     */
    @PostMapping("/role/findPermission/{roleid}")
    public List<Permission> findBySysPermissionRoleId(@PathVariable("roleid")Long roleid) {
        return roleService.findBySysPermissionRoleIds(roleid);
    }

}
